package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.ChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional(rollbackFor = RuntimeException.class)
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {

    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Override
    public ResponseResult channelList() {
        //安全保障 构造分页
        Page<WmChannel> page = new Page<>(1, 100);
        //列表查询
        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(page, null);
        //返回数据
        return ResponseResult.okResult(wmChannelPage.getRecords());
    }

    @Override
    public ResponseResult channelSave(WmChannel wmChannel) {
        //入参判空
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        String name = wmChannel.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmChannel::getName, name);
        Integer count = wmChannelMapper.selectCount(wrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道不能重名");
        }
        //频道创建时间
        wmChannel.setCreatedTime(new Date());
        //WmChannelServiceImpl.save = this.save用的是ServiceImpl<WmChannelMapper, WmChannel>里的save方法
        boolean saveResult = this.save(wmChannel);
        return saveResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public PageResponseResult findList(ChannelDto channelDto) {
        //判空
        if (channelDto == null) {
            return new PageResponseResult(0, 0, 0);
        }
        Integer page = channelDto.getPage();
        Integer size = channelDto.getSize();
        //设置页码和长度的默认值
        page = page == null ? 1 : page;
        size = size == null ? 10 : size;
        //构建分页规则
        Page<WmChannel> pageInfo = new Page<>(page, size);
        //组装查询条件
        String name = channelDto.getName();
        LambdaQueryWrapper<WmChannel> wrapper = new LambdaQueryWrapper<>();
        //StringUtils.isNotBlank(name) 判断条件，符合才会往后进行
        wrapper.like(StringUtils.isNotBlank(name), WmChannel::getName, name);
        wrapper.orderByDesc(WmChannel::getCreatedTime);

        Page<WmChannel> pageResult = wmChannelMapper.selectPage(pageInfo, wrapper);

        Long total = pageResult.getTotal();
        PageResponseResult pageResponseResult = new PageResponseResult(page,size,total.intValue());
        //总条数
        pageResponseResult.setData(pageResult.getRecords());

        return pageResponseResult;
    }

    @Override
    public ResponseResult channelUpdate(WmChannel wmChannel) {
        //判断入参
        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断重名
        String name = wmChannel.getName();
        if (StringUtils.isEmpty(name)) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"频道不能为空");
        }
        //构造查询条件
        LambdaQueryWrapper<WmChannel> channelWrapper = new LambdaQueryWrapper<>();
        channelWrapper.eq(WmChannel::getName, name);
        Integer count = wmChannelMapper.selectCount(channelWrapper);
        if (count > 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST, "频道不能重名");
        }
        //判断当前频道是否包含新闻内容
        LambdaQueryWrapper<WmNews> newWrapper = new LambdaQueryWrapper<>();
        newWrapper.eq(WmNews::getChannelId,wmChannel.getId());
        newWrapper.eq(WmNews::getStatus,9);
        //查询新闻内容
        Integer newCount = wmNewsMapper.selectCount(newWrapper);
        if (newCount  > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道关联了新闻，无法修改");
        }
        //修改
        boolean updateResult = this.updateById(wmChannel);
        return updateResult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }

    @Override
    public ResponseResult delete(Integer id) {
        //判断入参
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //查询频道信息
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel == null){
            return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
        }
        //判断频道状态是否有效
        Boolean status = wmChannel.getStatus();
        if (status){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道有效，不能删除");
        }
        //检测频道状态下是否有新闻
        LambdaQueryWrapper<WmNews> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(WmNews::getChannelId,wmChannel.getId());
        wrapper.eq(WmNews::getStatus,9);

        Integer selectCount = wmNewsMapper.selectCount(wrapper);
        if (selectCount > 0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"频道关联了新闻，无法删除");
        }
        //删除频道
        boolean removeByIdresult = this.removeById(id);
        return removeByIdresult ? ResponseResult.okResult(AppHttpCodeEnum.SUCCESS) : ResponseResult.errorResult(AppHttpCodeEnum.SERVER_ERROR);
    }
}